<?php

class CommonAction extends Action {

    protected $_action;

    public function _initialize() {
        $this->_action = MODULE_NAME;
        $this->assign('act', MODULE_NAME);
        $this->assign('meth', ACTION_NAME);
        $this->assign('isLogin', 1); //是否登录
        // set layout 'LAYOUT_ON' => TRUE,
        C('LAYOUT_NAME', 'Layout/boot');
        $layout = $this->_get('layout');
        if ($layout) {
            C('LAYOUT_NAME', 'Layout/' . $layout);
        }

        //设置缓存时间
        C('DATA_CACHE_TIME', 300);

        //$this->notice('请先登录');exit;
        $allow = array('Index_checkLogin', 'Index_logout');
        if (!isset($_SESSION['user_id']) && !in_array(MODULE_NAME . '_' . ACTION_NAME, $allow)) {

            C('LAYOUT_NAME', 'Layout/boot_empty');
            $this->display('Admin:Index:login');
            exit;
        }
        $this->assign('username', $_SESSION['account']);
        $this->assign('user_id', $_SESSION['user_id']);
        $this->assign('lastLoginTime', $_SESSION['lastLoginTime']);
    }

    /**
     *
     * 取得文件上传对象实例
     * @access protected
     */
    protected function _upload($path) {
        if (!empty($_FILES)) {
            $savePath = C('UPLOAD_PATH_PRODUCT');
            if(!is_null($path)) {
                $savePath = $path;
            }
            debug($savePath);
            import("@.ORG.Net.UploadFile");
            import("@.ORG.Util.Image");
            $upload = new UploadFile();
            $upload->savePath =  $savePath;// 设置附件上传目录
            $upload->maxSize  = 314572822 ;// 设置附件上传大小
            $upload->allowExts  = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
            $upload->thumb = true;
            $upload->uploadReplace = true;
            $upload->thumbMaxWidth = '50,200';
            $upload->thumbMaxHeight = '50,200';
            if (!$upload->upload()) {
                debug($upload->getErrorMsg());
                $this->error($upload->getErrorMsg());
            } else {
                $info = $upload->getUploadFileInfo();
                return $path . $info[0]['savename'];
            }
        }
    }



    /**    写入数据
     * 
     */
    public function insert() {
        $this->_insert();
    }

    public function _insert() {

        $model = D($this->_action);
        $this->_before_insert($model);
        if ($vo = $model->create()) {
            $lastInsertId = $model->add();
            $this->_after_insert($lastInsertId);
            if ($lastInsertId !== false) {
                $this->clearRedisByAction();
                $this->success('数据保存成功！');
            } else {
                $this->error('数据写入错误！');
            }
        } else {
            $this->error($model->getError());
        }
    }

    public function clearRedisByAction($group='') {
        if($group){
            $groupName=$group;
        }else{
            $groupName = (C('REDIS_SITE') . $this->_action);
        }
        $clears = S($groupName);
        foreach ($clears as $clear) {
            S($clear, NULL);
        }
    }

    protected function _before_insert(&$model) {
        //插入前的数据转换
    }

    protected function _after_insert($lastInsertId) {
        //插入后的信息提示或者操作
    }
    public function add(){
        $code = getCode(3,1) . dec2Any(time(), 62) . getCode();
        $this->assign('code', $code);
        $this->display();
    }
    function edit() {

        $this->_edit();
    }

    public function _edit() {
        //$name = __ACTION__;
        $id = (int) $this->_request('id');
        $this->_before_edit($id);
        $model = M($this->_action);

        $row = $model->where($model->getPk() . '=' . $id)->find();
        //$row = $model->where($model->getPk() . '=' . $id)->cache(true,'360000','',$this->_action)->find();
        $this->assign('row', $row);
        $this->assign('id', $id);
        $this->_after_edit($row);
        $this->display();
    }

    protected function _before_edit($id) {

        //
    }

    protected function _after_edit($row) {
        //
    }

    public function update() {
        $this->_update();
    }

    public function _update() {

        $this->_before_update();
        $model = D($this->_action);
        if ($vo = $model->create()) {
            $result = $model->save();
            $this->_after_update($result);
            if ($result !== false) {
                $this->clearRedisByAction();
                $this->success('数据更新成功！');
            } else {
                $this->error("没有更新任何数据!");
            }
        } else {
            $this->error($model->getError());
        }
    }

    protected function _before_update($model) {
        //
    }

    protected function _after_update($result) {
        //
    }

    // 删除数据
    public function delete() {
        $this->_delete();
    }

    public function _delete() {
        $ids = $this->_request('ids');
        if (!empty($ids)) {
            $this->_before_delete($ids);
            $model = M($this->_action);
            $model->startTrans();
            if (!is_array($ids)) {

                $result = $model->delete((int) $ids);
            } else {
                foreach ($ids as $id) {
                    $result = $model->delete((int) $id);
                    if (false !== $result) {
                        break;
                    }
                }
            }

            if (false !== $result) {
                $model->commit();
                $this->_after_delete($ids);
                $this->clearRedisByAction();
                $this->success('删除成功！', __URL__);
            } else {
                $model->rollback();
                $this->error('删除出错！');
            }
        } else {
            $this->error('删除项不存在！');
        }
    }

    protected function _before_delete($ids) {
        //
    }

    protected function _after_delete($ids) {
        //
    }


    public function getTimeInterval($timestamp = false) {
        $timeInterval = array();
        $yesterday = date('Y-m-d', time() - 3600 * 24);
        $begin = $this->_request('begin');
        $end = $this->_request('end');
        if ($begin == $yesterday) {
            $end = date('Y-m-d', time());
        }
        if (!$end && !$begin) {
            return false;
        }
        if ($begin && empty($end)) {
            $timeInterval = array('egt', $begin);
        }

        if ($end && empty($begin)) {
            $timeInterval = array('elt', $end);
        }
        if ($end && $begin) {
            $timeInterval = array('between', array($begin, $end));
        }
        return $timeInterval;
    }

}